function jump(obj) {
    document.getElementsByClassName("layui-nav-item layui-this")[0].className = "layui-nav-item";
    obj.className = "layui-nav-item layui-this";
    let target = obj.innerText;
    let str = "";
    localStorage.setItem("nav", target);

    switch (target) {
        case "首页":
            str = "index";
            break;
        case "活动":
            str = "activity"
            break;
        case "电影":
            str = "movies";
            break;
        case "会员":
            str = "vip";
            break;
        case "个人中心":
            str = "personal_center";
            break;
        case "登录":
            str = "login";
            break;
        case "消息":
            window.location.href = `../html/personal_center.html?sg=6`
            break;
    }
    
    window.location.href = `../html/${str}.html`
}

// 判断是否登录
$(function () {
    let account = localStorage.getItem("token");

    if (account != null) {
        $("#outLogin").css("display", "")
        $("#login_byn").css("display", "none")
        $("#navLogin").css("display", "none")
        $("#message").css("display", "")
    } else {
        $("#outLogin").css("display", "none")
        $("#login_byn").css("display", "")
        $("#navLogin").css("display", "")
        $("#message").css("display", "none")
    }

})

// 退出登录
function outLogin() {
    localStorage.removeItem('token')
    $("#outLogin").css("display", "none")
    $("#login_byn").css("display", "")
    $("#message").css("display", "none")
    $("#navLogin").css("display", "")
}

let userInfo
$(function () {
    let layui_nav_item = document.getElementsByClassName("layui-nav-item");
    layui_nav_item[0].className = "layui-nav-item layui-this"
    let nav = localStorage.getItem("nav")
    localStorage.removeItem('nav')
   
    let layui_this = document.getElementsByClassName("layui-this")[0];
    layui_this.className = 'layui-nav-item';
    switch (nav) {
        case "首页":
            layui_nav_item[0].className = "layui-nav-item layui-this"
            break;
        case "活动":
            layui_nav_item[2].className = "layui-nav-item layui-this"
            break;
        case "电影":
            layui_nav_item[1].className = "layui-nav-item layui-this"
            break;
        case "会员":
            layui_nav_item[3].className = "layui-nav-item layui-this"
            break;
        case "登录":
            layui_nav_item[4].className = "layui-nav-item layui-this"
            break;
        case null:
            layui_nav_item[5].className = "layui-nav-item layui-this"
            break;
        case "个人中心":
            layui_nav_item[6].className = "layui-nav-item layui-this"
            break;
    }
        $.ajax({
            headers: {
                "token": localStorage.getItem("token")
            },
            type: 'get',
            url: serverConfig.serverUrl + '/devs/user',
            async: false,
            success: function (result) {
                if (result.code == 200)
                    userInfo = result.data;
            }
        })
        if (userInfo != null && userInfo.memberId != null) {
            document.querySelector("#huiyuan").innerText = "";
        }
        
        if(userInfo != null){
            $.ajax({
                type: 'get',
                url: serverConfig.serverUrl + '/devs/message',
                data: {userId: userInfo.id},
                async: false,
                success: function (result) {
                    document.getElementById("number").innerText = result.data;
    
                    // 监听按钮的点击事件
                    document.getElementById('message').addEventListener('click', function () {
    
                        $.ajax({
                            type: 'post',
                            url:serverConfig.serverUrl + '/devs/message',
                            data: {userId: userInfo.id},
                            async: false,
                            success: function (result) {
                                // window.location.href = "";
                            }
                        })
                    });
    
                }
            })
        }
        return userInfo;
    }

)

$(function () {
    if(userInfo != null){
let ws;
    //检测浏览器是否支持webSocket
    if ("WebSocket" in window) {

        // $("#message").html("您的浏览器支持webSocket!");
        //模拟产生clientID
        let clientID = userInfo.id;
        console.log(clientID)

        //创建 WebSocket 对象,注意请求路径！！！！
        //部署的时候,需要修改成真实的ip地址
        ws = new WebSocket("ws://118.178.105.195:8090/dev/socket/" + userInfo.id);

        //与服务端建立连接时触发
        ws.onopen = function () {
            console.log(1)

            //模拟发送数据到服务器
            ws.send("你好服务端！我是客户端8090 支付    id:" + userInfo.id);
        }
        //接收到服务端消息时触发
        ws.onmessage = function (evt) {
            let received_msg = evt.data;
            console.log(received_msg)
            // document.getElementById("number").innerText = document.getElementById("number").innerText - ( -1)
            $.ajax({
                type: 'get',
                url:serverConfig.serverUrl + '/devs/message',
                data: {userId: userInfo.id},
                async: false,
                success: function (result) {
                    document.getElementById("number").innerText = result.data;
                }
            })
        };

        //服务端关闭连接时触发
        //服务端关闭连接时触发
        ws.onclose = function () {
            console.error("连接已经关闭.....")
        };
    } else {
        $("#content").html("您的浏览器不支持webSocket！");
    }
    }
    

})